package day0213;

public class SyncDemo2 {
    //public static Object obj1111 = new Object();
    public static  Shop shop = new Shop();
    public static String canUse_person="王克晶";
    public static void main(String[] args) throws InterruptedException {

        //Runnable方式创建匿名内部类线程对象。
        Thread t1 = new Thread("范传奇"){
            public void run() {
                try {
                    System.out.println("线程t1--范传奇--开始执行");
                    synchronized (shop) {
                        if(canUse_person.equals("王克晶")){
                            System.out.println("线程t1--范传奇--进入等待");
                            canUse_person="范传奇";
                            shop.wait();
                            System.out.println("线程t1--范传奇--等待结束--------over-over(线程t1运行完毕)");
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };



        Thread t2 = new Thread( ()->{
            try {
                System.out.println("线程t2--王克晶--开始执行");
                synchronized (shop) {
                    if(canUse_person.equals("范传奇")){
                        System.out.println("线程t2(王克晶)---唤醒线程t1(范传奇)");
                        /*Scanner scanner = new Scanner(System.in);
                        String str = scanner.nextLine();*/
                        shop.notify(); // 需要持有锁后再通知
                        Thread.sleep(100);
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"王克晶");
        t1.start();
        t2.start();

    }
}








